package com.testing.redboy.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.testing.redboy.FirstActivity;
import com.testing.redboy.R;
import com.testing.redboy.bean.VPBean;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;


public class HomeFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private ImageView p1;
    private ImageView p2;
    private ImageView p3;
    private ImageView p4;
    private ImageView p5;
    private ImageView[] list;
    private ViewPager vp;
    //   private int[] vp_im = new int[]{R.drawable.im1, R.drawable.im2,R.drawable.im3,R.drawable.im4,R.drawable.im5};
    private ListView lv;
    private int[] lv_im = new int[]{R.drawable.home_classify_01, R.drawable.home_classify_02, R.drawable.home_classify_03, R.drawable.home_classify_04, R.drawable.home_classify_05};
    private String[] lv_class = new String[]{"限时抢购", "促销快报", "新品上架", "热门单品", "推荐品牌"};


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            int currentItem = vp.getCurrentItem();

            vp.setCurrentItem(++currentItem);

            handler.sendEmptyMessageDelayed(1, 2000);
        }
    };
    private TextView search_butt;

    public HomeFragment() {
    }

    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home, container, false);

        initView(v);

        list = new ImageView[]{p1, p2, p3, p4, p5};

        changePoint(0);

        setData();

        vp.setCurrentItem(1000);

        handler.sendEmptyMessageDelayed(1, 2000);


        lv.setAdapter(new Mybs(getActivity(), lv_class));

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Intent intent0 = new Intent(getActivity(), FirstActivity.class);
                        intent0.putExtra("fk", 5);
                        startActivity(intent0);
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        Intent intent1 = new Intent(getActivity(), FirstActivity.class);
                        intent1.putExtra("fk", 6);
                        startActivity(intent1);
                        break;
                }
            }
        });

        search_butt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent0 = new Intent(getActivity(), FirstActivity.class);
                intent0.putExtra("fk", 8);
                startActivity(intent0);
            }
        });

        return v;
    }

    private void initView(View v) {
        p1 = (ImageView) v.findViewById(R.id.p1);
        p2 = (ImageView) v.findViewById(R.id.p2);
        p3 = (ImageView) v.findViewById(R.id.p3);
        p4 = (ImageView) v.findViewById(R.id.p4);
        p5 = (ImageView) v.findViewById(R.id.p5);

        vp = (ViewPager) v.findViewById(R.id.vp);

        lv = (ListView) v.findViewById(R.id.lv);

        search_butt = (TextView) v.findViewById(R.id.search_butt);
    }

    public void changePoint(int index) {
        for (int i = 0; i < lv_im.length; i++) {
            if (i == index) {
                list[i].setImageResource(R.drawable.slide_adv_selected);
            } else {
                list[i].setImageResource(R.drawable.slide_adv_normal);
            }
        }
    }

    class Mybs extends BaseAdapter {

        Context context;
        String[] al;
        private ViewH vv;

        public Mybs(Context context, String[] al) {
            this.context = context;
            this.al = al;
        }

        @Override
        public int getCount() {
            return al.length;
        }

        @Override
        public Object getItem(int position) {
            return al[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.home_lv_item, null);
                vv = new ViewH();
                vv.im = (ImageView) convertView.findViewById(R.id.imim);
                vv.tv = (TextView) convertView.findViewById(R.id.tvtv);
                convertView.setTag(vv);
            } else {
                vv = (ViewH) convertView.getTag();
            }

            vv.im.setImageResource(lv_im[position]);
            vv.tv.setText(al[position]);

            return convertView;
        }

        class ViewH {
            ImageView im;
            TextView tv;
            ImageView im2;
        }
    }

    public void setData() {
        String u = "http://120.27.126.103:8080/ECServer_D/home";
        RequestParams params = new RequestParams(u);
        x.http().get(params, new Callback.CommonCallback<String>() {

            private List<VPBean.HomeBannerBean> list;

            @Override
            public void onSuccess(String result) {
                Gson g = new Gson();
                VPBean vb = g.fromJson(result, VPBean.class);
                list = vb.home_banner;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                //
                vp.setAdapter(new PagerAdapter() {
                    @Override
                    public int getCount() {
                        return Integer.MAX_VALUE;
                    }

                    @Override
                    public boolean isViewFromObject(View view, Object object) {
                        return view == object;
                    }

                    @Override
                    public Object instantiateItem(ViewGroup container, int position) {

                        ImageView iv = new ImageView(getActivity());
                        iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                        iv.setScaleType(ImageView.ScaleType.FIT_XY);
                        int num = position % list.size();
                        x.image().bind(iv, "http://120.27.126.103:8080/ECServer_D/" + list.get(num).pic);

                        container.addView(iv);

                        return iv;
                    }

                    @Override
                    public void destroyItem(ViewGroup container, int position, Object object) {
                        container.removeView((View) object);
                    }
                });

                //
                vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        int index = position % list.size();
                        changePoint(index);
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
            }
        });
    }
}
